﻿using AutoMapper;
using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace SalesManagementWrite.API.SalesManagementWrites.BillingInvoicing
{
    public class BillingInvoicingCreateHandler:IRequestHandler<BillingInvoicingCreateCommon,Result>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Invoicingmemo> invoicingmemobaseRepository;

        public BillingInvoicingCreateHandler(IMapper mapper, IBaseRepository<Invoicingmemo> invoicingmemobaseRepository)
        {
            this.mapper = mapper;
            this.invoicingmemobaseRepository = invoicingmemobaseRepository;
        }

        /// <summary>WW
        /// 开票通知单添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(BillingInvoicingCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var invoic = mapper.Map<Invoicingmemo>(request);
                invoic.CreatedOwnerId = "张三";
                invoic.CreatedTime = DateTime.Now;
                invoic.Auditor = "李四";
                invoic.AuditDate = DateTime.Now;
                var list =await invoicingmemobaseRepository.Add(invoic);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "开票通知单添加成功";
                }
                else
                {
                    result.Msg = "开票通知单添加失败";
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
